<?php 
	class db_mongo implements db_interface{

		protected $database_name;
		protected $conn;
		protected $collection;
		protected $collection_obj;
		
		public function __construct(){
			$config 				= Yaf_Application::app()->getConfig();
			//在配置文件中应该有一项配置 如：database_name = 'ym'
			$this->database_name 	= $config['database_name'];
			//下面会得到当前使用的collection的名称
			$this->collection 		= str_replace('Model', '', get_class($this));

			$this->conn = new Mongoclient("mongodb://root:root@127.0.0.1:27017");
			$this->collection_obj = $this->conn->selectCollection( $this->database_name , $this->collection );
		}	

		public function insert($arr){
			return $this->collection_obj->insert($arr);
		}

		public function edit($arr){}

		public function select($arr=array(),$sort_arr=array(),$limit=null){
			$res = $this->collection_obj->find($arr)->sort($sort_arr)->limit($limit);
			
			if($res->count() == 0){
				$return = array();
			
			}
			foreach ($res as $key => $value) {
				$return[] = $value;
			}
			return $return;
		}

		public function delete($arr){
			if(isset($arr['_id'])){
				$arr['_id'] = new MongoId($arr['_id']);
			}
			return $this->collection_obj->remove($arr);
		}
	}
?>
